#include "../../../common/mysql_message.hpp"
#include "../../../odb/user.hxx"
#include "message-odb.hxx"
#include <mysql/mysql.h>
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试; true-发布;");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志输出等级");  //trace default 0

void insert_test(xchat::MessageTable &message_table)
{
    // boost::posix_time::ptime now1 = boost::posix_time::second_clock::local_time();
    // xchat::Message m1("消息ID1", "会话ID1", "用户ID1", 0, now1);
    // message_table.insert(m1);
    // boost::posix_time::ptime now2 = boost::posix_time::second_clock::local_time();
    // xchat::Message m2("消息ID2", "会话ID1", "用户ID2", 0, now2);
    // message_table.insert(m2);
    // boost::posix_time::ptime now3 = boost::posix_time::second_clock::local_time();
    // xchat::Message m3("消息ID3", "会话ID1", "用户ID3", 0, now3);
    // message_table.insert(m3);
    // boost::posix_time::ptime now4 = boost::posix_time::second_clock::local_time();
    // xchat::Message m4("消息ID4", "会话ID2", "用户ID4", 0, now4);
    // message_table.insert(m4);
    // boost::posix_time::ptime now5 = boost::posix_time::second_clock::local_time();
    // xchat::Message m5("消息ID5", "会话ID2", "用户ID5", 0, now5);
    // message_table.insert(m5);
    // boost::posix_time::ptime now6 = boost::posix_time::second_clock::local_time();
    // xchat::Message m6("消息ID6", "会话ID2", "用户ID6", 0, now6);
    // message_table.insert(m6);
}
void remove_test(xchat::MessageTable &message_table)
{
    message_table.remove("会话ID2");
}
void recent_test(xchat::MessageTable &message_table)
{
    auto res = message_table.recent("会话ID2", 2);
    for (auto it = res.rbegin(); it != res.rend(); ++it) 
    {
        // it 是反向迭代器，*it 获取当前元素的引用
        const auto &m = *it;  // 注意：如果不需要修改，建议用 const 引用
        
        std::cout << m.message_id() << std::endl;
        std::cout << m.session_id() << std::endl;
        std::cout << m.user_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(m.create_time()) << std::endl;
    }
}
void range_test(xchat::MessageTable &message_table)
{
    boost::posix_time::ptime start_time = boost::posix_time::time_from_string("2025-09-30 10:39:00");
    boost::posix_time::ptime end_time   = boost::posix_time::time_from_string("2025-010-24 00:00:00");
    auto res = message_table.range("会话ID2", start_time, end_time);
    for(auto &m : res)
    {
        std::cout << m.message_id() << std::endl;
        std::cout << m.session_id() << std::endl;
        std::cout << m.user_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(m.create_time()) << std::endl;
    }
}
int main(int argc, char* argv[])
 {
    google::ParseCommandLineFlags(&argc, &argv, true);
    
    xchat::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    auto db = xchat::ODBFactory::create("root", "299274142", "127.0.0.1", "xchat", "utf8", 0, 1);

    xchat::MessageTable message_table(db);

    //insert_test(message_table);
    //remove_test(message_table);
    recent_test(message_table);
    //range_test(message_table);
    return 0;
 }